package infrastructure

import "github.com/speps/go-hashids"

type HashId struct {
	salt string
	hd *hashids.HashID
}

func NewHashId(salt string) *HashId {
	hd := new(HashId)
	hd.salt = salt
	cfg := hashids.NewData()
	cfg.Salt = hd.salt
	cfg.MinLength = 8
	hd.hd, _ = hashids.NewWithData(cfg)
	return hd
}

func (hd HashId) Encode(value int) (string, error) {

	var s string
	e, err := hd.hd.Encode([]int{value})
	if err != nil {
		return s, err
	}
	return e, nil
}

//Decode
func (hd HashId) Decode(data string) ([]int, error) {
	return hd.hd.DecodeWithError(data)
}

func (hd HashId) DecodeFirst(data string) (int, error) {
	var number int
	numbers, err := hd.Decode(data)
	if err != nil {
		return number, err
	}
	return numbers[0], nil
}